package eventplanner.ical;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.List;

import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.PropertyList;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.property.CalScale;
import net.fortuna.ical4j.model.property.Created;
import net.fortuna.ical4j.model.property.Description;
import net.fortuna.ical4j.model.property.Location;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Url;
import net.fortuna.ical4j.model.property.Version;
import net.fortuna.ical4j.util.HostInfo;
import net.fortuna.ical4j.util.UidGenerator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eventplanner.entities.Event;

public class ICalConverter {

	private static final Logger LOG = LoggerFactory.getLogger(ICalConverter.class);
	private static final HostInfo HOST_INFO = new HostInfo() {
		@Override
		public String getHostName() {
			return "eventplanner";
		}
	};

	public String convert(List<Event> events) {
		Calendar calendar = new Calendar();
		calendar.getProperties().add(Version.VERSION_2_0);
		calendar.getProperties().add(CalScale.GREGORIAN);
		calendar.getProperties().add(new ProdId("-//SharepointHakker//iCal4j 1.0//EN"));

		for (Event event : events) {
			VEvent vEvent = new VEvent(new DateTime(event.getStart()), new DateTime(event.getEnd()), event.getTitle());
			PropertyList properties = vEvent.getProperties();
			properties.add(new ProdId(event.getTitle()));
			properties.add(new UidGenerator(HOST_INFO, event.getTitle() + event.getStart()).generateUid());


			addDescription(event, properties);
			addCreatedOn(event, properties);
			addLocation(event, properties);
			addURL(event, properties);
			calendar.getComponents().add(vEvent);
		}

		return calendar.toString();
	}

	private void addURL(Event event, PropertyList properties) {
		String url = event.getUrl();
		if (url != null) {
			try {
				properties.add(new Url(new URI(url)));
			} catch (URISyntaxException e) {
				LOG.error("The url '{}' is not valid. Unable to add it to event '{}'", new Object[]{url, event.getTitle(), e});
			}
		}
	}

	private void addLocation(Event event, PropertyList properties) {
		String location = event.getLocation();
		if (location != null) {
			properties.add(new Location(location));
		}
	}

	private void addCreatedOn(Event event, PropertyList properties) {
		Date createdOn = event.getCreatedOn();
		if (createdOn != null) {
			properties.add(new Created(new DateTime(createdOn)));
		}
	}

	private void addDescription(Event event, PropertyList properties) {
		String description = event.getDescription();
		List<String> taglist = event.getTags();
		String total = description == null ? "" : description;

		if (taglist != null && taglist.size() > 0) {
			String tags = taglist.toString().replace("\\[", "").replace("\\]", "");
			total += "\n\nTags: " + tags;
		}

		properties.add(new Description(total));
	}
}
